<%@ page language="java" contentType="text/xml; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@page import="java.io.*,javax.xml.parsers.*,org.w3c.dom.*,java.sql.*"%>
<%!//处理字符串的方法：
	public String codeString(String s) {
		String str = s;
		try {
			byte b[] = str.getBytes("ISO-8859-1");
			str = new String(b);
			return str;
		} catch (Exception e) {
			return str;
		}
	}%>
<%
	try {
		DocumentBuilderFactory factory = DocumentBuilderFactory
				.newInstance();
		DocumentBuilder builder = factory.newDocumentBuilder();
		Document document = builder.parse(request.getInputStream());
		Node n = document.getElementsByTagName("type").item(0);
		if (n.getTextContent().equals("loadNotes")) {
			System.out.println("load");
%>
<%
	String message = "", logname = "", url = "";
			if (true) {
				Node node = document.getElementsByTagName("user").item(
						0);
				logname = node.getTextContent();
				if (logname == null) {
					logname = "";
				}
				logname = codeString(logname);

				node = document.getElementsByTagName("url").item(0);
				url = node.getTextContent();
				if (url == null) {
					url = "";
				}
				url = codeString(url);
				//System.out.println(logname + "," + url);
			}

			//////////////////
			try {
				Class.forName("com.mysql.jdbc.Driver");
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
			Connection con = null;
			Statement sql = null;
			ResultSet rs = null;
			try {
				con = DriverManager.getConnection("jdbc:mysql://localhost:3306/snp", "root", "123");
				sql = con.createStatement();
				/*
				 * String s=
				 * "'"+logname+"'"+","+"'"+realname+"'"+","+"'"+password+"'"
				 * +","+"'"+ email+"'"+","+"'"+phone+"'"+","+"'"+address+"'"; String
				 * condition="INSERT INTO user VALUES"+"("+s+")";
				 */
				String condition = "select * from notes where logname = '"
						+ logname + "' and " + "url = '" + url + "'";
				 System.out.println(condition);
				rs = sql.executeQuery(condition);
				message = "OK";
				
				
			} catch (SQLException e) {
				
				e.printStackTrace();
				message = "FALSE";
			}
%>

<response> <status><%=message%></status> <notes> <%
 	while (rs.next()) {
 %> <note><id><%=rs.getString("id")%></id><text><%=rs.getString("text")%></text><timestamp><%=rs.getString("timestamp")%></timestamp><left><%=rs.getString("snpleft")%></left><top><%=rs.getString("top")%></top><zindex><%=rs.getString("zindex")%></zindex> <url><%=rs.getString("url")%></url> </note> <%
 	}
 %> </notes> </response>
 <%con.close();%>
<%
	} else if (n.getTextContent().equals("addNotes")) {
			System.out.println("insert");
%>
<jsp:useBean id="insertnotes" class="com.InsertNotes" scope="session">
</jsp:useBean>
<%
	//提交信息后，验证信息是否正确：
			String message = "", id = "", logname = "", text = "", timestamp = "", left = "", top = "", zindex = "", url = "";
			if (!(session.isNew())) {
				Node node = document.getElementsByTagName("id").item(0);
				id = node.getTextContent();
				if (id == null) {
					id = "";
				}
				id = codeString(id);

				node = document.getElementsByTagName("user").item(0);
				logname = node.getTextContent();
				if (logname == null) {
					logname = "";
				}
				logname = codeString(logname);

				node = document.getElementsByTagName("text").item(0);
				text = node.getTextContent();
				if (text == null) {
					text = "";
				}
				text = codeString(text);

				node = document.getElementsByTagName("timestamp").item(
						0);
				timestamp = node.getTextContent();
				if (timestamp == null) {
					timestamp = "";
				}
				timestamp = codeString(timestamp);

				node = document.getElementsByTagName("left").item(0);
				left = node.getTextContent();
				if (left == null) {
					left = "";
				}
				left = codeString(left);

				node = document.getElementsByTagName("top").item(0);
				top = node.getTextContent();
				if (top == null) {
					top = "";
				}
				top = codeString(top);

				node = document.getElementsByTagName("zindex").item(0);
				zindex = node.getTextContent();
				if (zindex == null) {
					zindex = "";
				}
				zindex = codeString(zindex);

				node = document.getElementsByTagName("url").item(0);
				url = node.getTextContent();
				if (url == null) {
					url = "";
				}
				url = codeString(url);
			}
			System.out
					.println("values ('" + id + "','" + logname + "','"
							+ text + "','" + timestamp + "','" + left
							+ "','" + top + "','" + zindex + "','"
							+ url + "')");
%>
<%
	if (!(id.equals(""))) {
%>
<jsp:setProperty name="insertnotes" property="id" value="<%=id%>" />
<jsp:setProperty name="insertnotes" property="logname"
	value="<%=logname%>" />
<jsp:setProperty name="insertnotes" property="text" value="<%=text%>" />
<jsp:setProperty name="insertnotes" property="timestamp"
	value="<%=timestamp%>" />
<jsp:setProperty name="insertnotes" property="left" value="<%=left%>" />
<jsp:setProperty name="insertnotes" property="top" value="<%=top%>" />
<jsp:setProperty name="insertnotes" property="zindex" value="<%=top%>" />
<jsp:setProperty name="insertnotes" property="url" value="<%=url%>" />
<%
	insertnotes.insertItem();
			}
%>
<response> <status> <%=insertnotes.getMessage()%> </status> </response>
<%
	} else if (n.getTextContent().equals("updateNotes")) {
			System.out.println("update");
%>
<jsp:useBean id="updatenotes" class="com.UpdateNotes" scope="session">
</jsp:useBean>
<%
	//提交信息后，验证信息是否正确：
			String message = "", id = "", logname = "", text = "", timestamp = "", left = "", top = "", zindex = "", url = "";
			if (!(session.isNew())) {
				Node node = document.getElementsByTagName("id").item(0);
				id = node.getTextContent();
				if (id == null) {
					id = "";
				}
				id = codeString(id);

				node = document.getElementsByTagName("user").item(0);
				logname = node.getTextContent();
				if (logname == null) {
					logname = "";
				}
				logname = codeString(logname);

				node = document.getElementsByTagName("text").item(0);
				text = node.getTextContent();
				if (text == null) {
					text = "";
				}
				text = codeString(text);

				node = document.getElementsByTagName("timestamp").item(
						0);
				timestamp = node.getTextContent();
				if (timestamp == null) {
					timestamp = "";
				}
				timestamp = codeString(timestamp);

				node = document.getElementsByTagName("left").item(0);
				left = node.getTextContent();
				if (left == null) {
					left = "";
				}
				left = codeString(left);

				node = document.getElementsByTagName("top").item(0);
				top = node.getTextContent();
				if(top == null)
					top = "";
				top = codeString(top);

				node = document.getElementsByTagName("zindex").item(0);
				zindex = node.getTextContent();
				if (zindex == null) {
					zindex = "";
				}
				zindex = codeString(zindex);

				node = document.getElementsByTagName("url").item(0);
				url = node.getTextContent();
				if (url == null) {
					url = "";
				}
				url = codeString(url);
			}
			System.out
					.println("values ('" + id + "','" + logname + "','"
							+ text + "','" + timestamp + "','" + left
							+ "','" + top + "','" + zindex + "','"
							+ url + "')");
%>
<%
	if (!(id.equals(""))) {
%>
<jsp:setProperty name="updatenotes" property="id" value="<%=id%>" />
<jsp:setProperty name="updatenotes" property="logname"
	value="<%=logname%>" />
<jsp:setProperty name="updatenotes" property="text" value="<%=text%>" />
<jsp:setProperty name="updatenotes" property="timestamp"
	value="<%=timestamp%>" />
<jsp:setProperty name="updatenotes" property="left" value="<%=left%>" />
<jsp:setProperty name="updatenotes" property="top" value="<%=top%>" />
<jsp:setProperty name="updatenotes" property="zindex" value="<%=top%>" />
<jsp:setProperty name="updatenotes" property="url" value="<%=url%>" />
<%
	updatenotes.updateItem();
				System.out.println("after updateItem: values ('" + id + "','" + logname
						+ "','" + text + "','" + timestamp + "','"
						+ left + "','" + top + "','" + zindex + "','"
						+ url + "')");
			}
%>
<response> <status> <%=updatenotes.getMessage()%> </status> </response>
<%
	} else if (n.getTextContent().equals("removeNotes")) {
			System.out.println("remove");
%>
<jsp:useBean id="deletenotes" class="com.DeleteNotes" scope="session">
</jsp:useBean>
<%
	//提交信息后，验证信息是否正确：
			String message = "", id = "";
			if (!(session.isNew())) {
				Node node = document.getElementsByTagName("id").item(0);
				id = node.getTextContent();
				if (id == null) {
					id = "";
				}
				id = codeString(id);
			}
%>
<%
	if (!(id.equals(""))) {
%>
<jsp:setProperty name="deletenotes" property="id" value="<%=id%>" />
<%
	deletenotes.deleteItem();
			}
%>
<response> <status> <%=deletenotes.getMessage()%> </status> </response>
<%
	}
	} catch (Exception ex) {
		out.println(ex.getMessage());
	}
	//out.println("AAAAA:");
	//out.println(xml);
%>